<?php
namespace app\admin\controller;
use think\Controller;
/**
 * 后台管理登录管理
 * @author Yel 
 * @copyright  yel  2019/7/3
 */

class Login extends Controller{

	/**
	 * 登录
	 * @author Yel 
 	 * @copyright  yel  2019/7/3-4
	 */
	public function index(){
		if(request()->isPost()){
			//获取用户
			$account = input('param.account','');//账号
			$pwd = input('param.password','');//密码
			//第一次验证
			if(empty($account)){
				$this->result('',-1,'账号不能为空','json');
			}
			if(empty($pwd)){
				$this->result('',-1,'密码不能为空','json');
			}
			$userInfo = db('admin_users')->field('id,account,password,name,headimg,status')->where(['account'=>$account])->find();
			if(!$userInfo){
				$this->result('',-1,'账号或密码错误','json');
			}
			$op = password_verify($pwd,$userInfo['password']);
			if(!$op){
				$this->result('',-1,'账号或密码错误','json');
			}
			if($userInfo['status'] == 0){
				$this->result('',-1,'您的账号已经被冻结','json');
			}
			if(!$userInfo['headimg']){
				$userInfo['headimg'] = config('__HEADIMG_DEFAULT__');
			}
			//把资料存放在Session
			session('user',['id'=>$userInfo['id'],'name'=>$userInfo['name'],'headimg'=>$userInfo['headimg']]);
			$this->result('',200,'登录成功','json');
		}
		return view('index');
	}


	/**
	 * 退出登录
	 * @author Yel 
 	 * @copyright  yel  2019/7/5
	 */
	public function outlogin(){
		session(null);
		$this->redirect('admin/login/index');
	}

}